import VueI18n from 'vue-i18n'
import en_Locale from 'element-ui/lib/locale/lang/en'
import zh_CN_Locale from 'element-ui/lib/locale/lang/zh-CN'
import en from '@/i18n/lang/en';
import zh_CN from '@/i18n/lang/zh-cn';
import Vue from "vue";
import Element from "element-ui";
import Cookies from "js-cookie";
import { getLanguage } from "@/utils/auth";

function getElementLocale() {
  const language = getLanguage()
  switch (language) {
    case 'zh_CN':
      return zh_CN_Locale;
    case 'en_US':
      return en_Locale;
    default:
      return en_Locale;
  }
}

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
Vue.use(Element, {
  locale: getElementLocale(),
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.use(VueI18n, {
  // 解决前端报错传参为空的情况：[Vue warn]: Error in render: “TypeError: Cannot read property ‘_t’ of undefined”
  i18n: function (path, options) {
    let value = i18n.t(path, options);
    if (value !== null && value !== undefined) {
      return value;
    }
    return "";
  },
});

// 默认语言简体中文
const i18n = new VueI18n({
  locale: getLanguage(),
  messages: {
    zh_CN: {
      ...zh_CN_Locale,
      ...zh_CN
    },
    en_US: {
      ...en_Locale,
      ...en
    },
    silentTranslationWarn: false
  }
});

Vue.use(Element, {
  i18n: (key, value) => i18n.t(key, value),
})
export function exchageLanguage(lang) {
  i18n.locale = lang; // 更新 Vue-i18n 的 locale

  // 更新 Element UI 的本地化设置
  Vue.use(Element, {
    locale: lang === 'zh_CN' ? zh_CN_Locale : en_Locale,
    i18n: (key, value) => i18n.t(key, value),
  });


}
export default i18n
